package com.example.zk2_lx;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.zk2_lx.adapter.FriendAdapter;
import com.example.zk2_lx.databinding.FragmentFriendBinding;
import com.example.zk2_lx.friend.FriendViewModel;
import com.example.zk2_lx.mvvm_base.BaseFragment;

import java.util.ArrayList;
import java.util.List;


public class FriendFragment extends BaseFragment<FragmentFriendBinding, FriendViewModel> {
    List<String> friends = new ArrayList<>();
    FriendAdapter friendAdapter;


    public FriendFragment() {
        // Required empty public constructor
    }


    @Override
    protected int bindLayout() {
        return R.layout.fragment_friend;
    }

    @Override
    protected void initView() {
        friendAdapter = new FriendAdapter(R.layout.item_friend,friends);
        dataBinding.rv.setAdapter(friendAdapter);
        dataBinding.rv.setLayoutManager(new LinearLayoutManager(getContext()));

        viewModel.friend();
        //点击跳转
        friendAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                String s = friends.get(position);
                Bundle bundle = new Bundle();
                bundle.putInt("chatType",0);
                bundle.putString("conversationId",s);
                ARouter.getInstance().build("/app/ChatActivity").withBundle("bundle",bundle).navigation();
            }
        });

    }

    @Override
    protected void initData() {
        viewModel.success.observe(this, new Observer<List<String>>() {
            @Override
            public void onChanged(List<String> strings) {
                friends.clear();
                friends.addAll(strings);
                friendAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected Class<FriendViewModel> getViewModel() {
        return FriendViewModel.class;
    }
}